#ifndef SHENLAN_UGUI
#define SHENLAN_UGUI

float UnityGet2DClipping(float2 position, float4 clipRect)
{
    float2 inside = step(clipRect.xy, position.xy) * step(position.xy, clipRect.zw);
    return inside.x * inside.y;
}

float UnityGet2DClipping_UIMaskSolf_Frag(float4 mask, float4 clipRect)
{
    half2 m = saturate((clipRect.zw - clipRect.xy - abs(mask.xy)) * mask.zw);
    return m.x * m.y;
}

float4 UnityGet2DClipping_UIMaskSolf_Vert(float posClipW, float2 vertexXY, float4 clipRect,float nessX,float nessY)
{
    float2 pixelSize = posClipW;
    pixelSize /=  abs(mul((float2x2)UNITY_MATRIX_P, _ScreenParams.xy));
    float4 clampedRect = clamp(clipRect, -2e10, 2e10);
    float4 mask = half4(vertexXY * 2 - clampedRect.xy - clampedRect.zw, 0.25 / (0.25 * half2(nessX, nessY) + abs(pixelSize.xy)));
    return mask;
}

half UnityGet2DClipping_UIMaskSolfOneWay_Frag(half2 mask, half4 soft,half4 clipRect)
{
    half4 maskRange = half4(-mask.x, mask.x, mask.y, -mask.y);
    half4 m = saturate((clipRect.zzww - clipRect.xxyy - maskRange) * soft);
    return m.x * m.y * m.z * m.w;
}

float2 UnityGet2DClipping_UIMaskSolfOneWay_Vert_Mask(float2 vertexXY, float4 clipRect)
{
    float2 mask = vertexXY * 2 - clipRect.xy - clipRect.zw;
    return mask;
}

float4 UnityGet2DClipping_UIMaskSolfOneWay_Vert_Soft(float posClipW, float nessX, float nessY, float4 softOffset)
{
    float2 pixelSize = posClipW;
    pixelSize /= abs(mul((float2x2)UNITY_MATRIX_P, _ScreenParams.xy));
    float4 soft = 0.25 / (0.25 * float4(nessX * softOffset.xy, nessY * softOffset.zw) + pixelSize.xxyy);
    return soft;
}
half4 UnityGetUIDiffuseColor(float2 position, TEXTURE2D_PARAM(main, mainTexture), TEXTURE2D_PARAM(alpha, alphaTexture), half4 textureSampleAdd)
{
    return half4(SAMPLE_TEXTURE2D(main, mainTexture, position).rgb + textureSampleAdd.rgb, SAMPLE_TEXTURE2D(alpha, alphaTexture, position).r + textureSampleAdd.a);
}

half4 DissolveUI(half4 tex, half dismap, half4 insideColor, half4 outsideColor, half4 dissolveOffset)
{
    half2 dissolveAlpha = (dismap - dissolveOffset.xx) * dissolveOffset.yz;
    tex.a = clamp(tex.a + dissolveAlpha.y, 0, tex.a);
    half dissolveRange = 1 - abs(dissolveAlpha.x);
    half4 edgeColor = insideColor * saturate(dissolveRange);
    tex.rgb = lerp(tex.rgb, outsideColor.rgb, step(1, -dissolveAlpha.x));
    tex.rgb += edgeColor.rgb;

    return tex;
}

half2 FishEyeToUV(half2 uv, half4 fisheyeOffset)
{
    half dis = length(uv - fisheyeOffset.xy) * fisheyeOffset.z;
    half2 preUV = ((uv - fisheyeOffset.xy) * sin(1.5708 * dis)) / dis + fisheyeOffset.xy;
    uv = lerp(uv, preUV, -fisheyeOffset.z * pow(dis, fisheyeOffset.w));
    return uv;
}

half2 FishEyeToUV_WorldSpace(half2 uv, half4 fisheyeOffset, half2 worldpos)
{
    half dis = length(fisheyeOffset.xy - worldpos) * fisheyeOffset.z;
    half2 center = uv + normalize(fisheyeOffset.xy - worldpos) * dis;
    half2 preUV = ((uv - center) * sin(1.5708 * dis)) / dis + center;
    uv = lerp(uv, preUV, -fisheyeOffset.z * pow(abs(dis), fisheyeOffset.w));
    return uv;
}
#endif